package com.yidao.menu.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.yidao.app.domain.AppContext;
import com.yidao.menu.domain.Menu;
import com.yidao.menu.domain.MenuItem;
import com.yidao.user.domain.UserSession;

@Controller
public class MenuController implements BeanFactoryAware{
	
	private Logger log = Logger.getLogger(MenuController.class);
	
	@Autowired
	private AppContext appContext;
	
	private BeanFactory beanFactory;
	
	public AppContext getAppContext() {
		return appContext;
	}

	@RequestMapping("/menu/view.do")
	public ModelAndView menu(){
		
		ModelAndView mv = new ModelAndView();
		Menu menu =  (Menu)appContext.getContextValue(AppContext.PROP_MENU);
		mv.addObject(AppContext.PROP_MENU, appContext.getContextValue(AppContext.PROP_MENU));
		mv.setViewName("/components/menu");
		UserSession userSession = (UserSession)this.beanFactory.getBean("userSession");
		
		//set default user selected menu.
		if(userSession.getSelectedMenuItem() == null){
			if(menu.getDefaultSubItemId() != null){
				userSession.setSelectedMenuItem(menu.getItemById(menu.getDefaultSubItemId()));
			}else{
				userSession.setSelectedMenuItem(menu.getMenuItems().get(0));
			}
		}
		mv.addObject("userSession", userSession);
		
		return mv;
	}
	
	@RequestMapping("/menu/action.do")
	public ModelAndView action(@RequestParam("id") String id){
		
		Menu menu = (Menu)appContext.getContextValue(AppContext.PROP_MENU);
		MenuItem menuItem = menu.getItemById(id);
		
		if(log.isDebugEnabled()){
			log.debug("selected menuItem:" + menuItem);
		}
		
		UserSession userSession = (UserSession)this.beanFactory.getBean("userSession");
		userSession.setSelectedMenuItem(menuItem);
		
		RedirectView view = new RedirectView(menuItem.getValue());
		view.setContextRelative(true);
		ModelAndView mv = new ModelAndView();
		
		mv.setView(view);
		
		return mv;
	}
	

	@RequestMapping("/menu/action1.do")
	public ModelAndView action1(@RequestParam("id") String id){
		
		Menu menu = (Menu)appContext.getContextValue(AppContext.PROP_MENU);
		MenuItem menuItem = menu.getItemById(id);
		
		if(log.isDebugEnabled()){
			log.debug("selected menuItem:" + menuItem);
		}
		
		UserSession userSession = (UserSession)this.beanFactory.getBean("userSession");
		userSession.setSelectedSubMenuItem(menuItem);
		
		RedirectView view = new RedirectView(menuItem.getValue());
		view.setContextRelative(true);
		ModelAndView mv = new ModelAndView();
		
		mv.setView(view);
		
		return mv;
	}
	
	@RequestMapping("/menu/subview.do")
	public ModelAndView submenu(){
		ModelAndView mv = new ModelAndView();
		Menu menu = (Menu)appContext.getContextValue(AppContext.PROP_MENU);
		UserSession userSession = (UserSession)this.beanFactory.getBean("userSession");
		MenuItem menuItem = userSession.getSelectedMenuItem();
		
		//set default user selected menu item.
		if(userSession.getSelectedSubMenuItem() == null){
			if(menuItem.getDefaultSubItemId() != null){
				userSession.setSelectedSubMenuItem(menu.getItemById(menuItem.getDefaultSubItemId()));
			}else{
				userSession.setSelectedSubMenuItem(menuItem.getSubMenuItems().get(0));
			}
		}
		
		mv.setViewName("/components/submenu");
		mv.addObject("userSession", userSession);
		
		return mv;
	}
	
	public void setAppContext(AppContext appContext) {
		this.appContext = appContext;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
}
